/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import BO.Iter.DeThiBO;

import BO.Iter.MonHocBO;
import BO.Iter.TraLoiBO;
import TableModel.Dapan;
import TableModel.Dethi;

import TableModel.Monhoc;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;


/**
 *
 * @author vt
 */
public class dethiController {
    public static final String TAG = "TAG at Dethi Controller ";

     private TraLoiBO tlBO;
    private DeThiBO  dtBO;
    private MonHocBO mhBO;
    private Dethi dethi;
    
    private Dapan[] listTraLoi;

    
    public DeThiBO getDtBO() {
        return dtBO;
    }
    public void setDtBO(DeThiBO dtBO) {
        this.dtBO = dtBO;
    }
    public Dapan[] getListTraLoi() {
        return listTraLoi;
    }

    public void setListTraLoi(Dapan[] listTraLoi) {
        this.listTraLoi = listTraLoi;
    }
    public MonHocBO getMhBO() {
        return mhBO;
    }

    public void setMhBO(MonHocBO mhBO) {
        this.mhBO = mhBO;
    }
    public Dethi getDethi() {
        return dethi;
    }
  
    public void setDethi(Dethi dethi) {
        this.dethi = dethi;
    }
    public TraLoiBO getTlBO() {
        return tlBO;
    }
    public void setTlBO(TraLoiBO tlBO) {
        this.tlBO = tlBO;
    }
  public void addAllTraLoi(List<Dapan> list){
      if(!list.isEmpty()){
          for(Dapan item : list){
              tlBO.add(item);
          }
      }
  }
  public List<Dethi> getAll(){
     return  dtBO.findAllDeThi();
  }
  
  
  public void getRequest(){
        HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
          dethi.setMamh((String)req.getAttribute("id_monhoc"));
          dethi.setMaLevel((String)req.getAttribute("id_level"));
          String  soCauTraloi=(String)req.getAttribute("number_cauhoi");      
          String ngaytaoTam = (String)req.getAttribute("ngaytao");
          SimpleDateFormat format= new SimpleDateFormat("dd/MM/yyyy");
        try {
            dethi.setNgaytao(format.parse(ngaytaoTam)); 
        } catch (ParseException ex) {
            Logger.getLogger(dethiController.class.getName()).log(Level.SEVERE, null, ex);
        }
          dethi.setSodapan(Integer.parseInt(soCauTraloi));
          listTraLoi = new Dapan[Integer.parseInt(soCauTraloi)];
          for(int i =0; i< Integer.parseInt(soCauTraloi); ++i){
              Dapan a = new Dapan();
              listTraLoi[i] = a;
          }
     
   }
  public String actionNew(){
      return "dtadd";
  }
  public String actionEdit(){
      return "editdethi";
  }
  public String actionCommit(){
     long index =  dtBO.add(dethi);
      for(int i =0 ; i< listTraLoi.length; ++i){
          Dapan a = listTraLoi[i];
          a.setMade(index + "");
          tlBO.add(a);
      }
      return "dtshow";
  }
  public String getTenMH(String mamh){
      Monhoc d = mhBO.findById(mamh);
      return d.getTenmh();
  }
   public Collection getAllMonHoc(){
      ArrayList arr = new ArrayList();
      List<Monhoc> list = mhBO.listAllMonHoc();
      for(Monhoc item : list){
           SelectItem a = new SelectItem(item.getMamh(),item.getTenmh().toString(), item.getGhiChu());
           arr.add(a);
      }
      return arr;
    }
}
